//
//  BaseViewController.m
//  MasonryDemo
//
//  Created by 谢英泽 on 2016/11/11.
//  Copyright © 2016年 谢英泽. All rights reserved.
//

#import "BaseViewController.h"
#import "CustomLoadingView.h"
#import "CustomAlertView.h"

#define kNav_BackIcon                           @"dd_unity_naviBack"
#define kImage_WithoutData                      @"unity_notData"

@interface BaseViewController ()

@property (nonatomic, strong) UIView *backView;
/** 自定义导航栏下划线 */
@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) CustomAlertView *alertView;

@property (nonatomic, strong) CustomLoadingView *loadingView;

@end

@implementation BaseViewController

#pragma mark - *********************生命周期*********************

- (void)viewDidLoad {
    [super viewDidLoad];
    //基本设置
    [self baseConfiguration];
    //默认显示自定义导航栏
    [self showCustomNavView];
    //子类基本配置
    [self configuration];
    [self initSubViews];
    [self autoLayoutForSubViews];
    [self bindViewModel];
    [self fetchData];
}

- (void)dealloc {
    NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));
}

#pragma mark - *********************基础配置*********************

- (void)baseConfiguration {
    //设置默认背景色
    self.view.backgroundColor = kCOLOR_tableView;
    //设置导航栏默认的起点为(0,0)
    self.navigationController.navigationBar.translucent = NO;
    //设置状态栏背景色跟内容一致
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    //设置导航栏颜色
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:kCOLOR_tableView] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    //开启系统侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    //设置系统导航栏右侧按钮
    [self setupBackNaviBar];
    
    [self hideNaviBack];
    
    //自定义导航栏左侧返回按钮
    @weakify(self);
    self.navView.clickCompleted = ^{
        @strongify(self);
        [self onNaviBack];
    };
}

#pragma mark - *********************基础方法*********************
/**
 设置系统导航栏返回按钮
 */
-(void)setupBackNaviBar {
    UIBarButtonItem *backBar = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:kNav_BackIcon] style:UIBarButtonItemStylePlain target:self action:@selector(onNaviBack)];
    [self.navigationItem setLeftBarButtonItem:backBar];
}

-(void)onNaviBack {
    if (_showTabBar) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotifi_showTabBarView object:nil];
    }
    [self finishNavi];
}

- (void)finishNavi {
    //实现导航右侧按钮实现方法
    if (self.navigationController) {
        if ([self.navigationController.viewControllers firstObject] == self) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
/**
 隐藏导航栏按钮
 */
-(void)hideNaviBack {
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.backBarButtonItem = nil;
}

#pragma mark - 自定义弹出窗

- (void)showAlertViewWithTitle:(NSString *)title content:(NSString *)content {
    CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:title content:content ensureButtonTitle:nil cancelButtonTitle:nil];
    [alertView show];
}

- (void)showSoonAlertViewWithTitle:(NSString *)title content:(NSString *)content {
    CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:title content:content ensureButtonTitle:nil cancelButtonTitle:nil];
    [alertView showAndDismissSoon];
}

- (void)showAlertViewWithTitle:(NSString *)title content:(NSString *)content  ensureButtonTitle:(NSString *)ensureButtonTitle cancelButtonTitle:(NSString *)cancelButtonTitle {
    CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:title content:content ensureButtonTitle:ensureButtonTitle cancelButtonTitle:cancelButtonTitle];
    self.alertView = alertView;
    [alertView show];
}

#pragma mark - 提示等待框

- (void)showLoading {
    [self.loadingView showLoading];
}

- (void)hideLoading {
    [self.loadingView hideLoading];
}

#pragma mark - 未登录跳转
/**
 未登录跳转
 */
- (void)gotoLoginVC {
    
}

#pragma mark - 自定义导航栏处理
/**
 显示自定义导航栏
 */
- (void)showCustomNavView {
    [self.view addSubview:self.navView];
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.height.equalTo(@64);
    }];
    [self.view bringSubviewToFront:self.navView];
}
/**
 隐藏自定义导航栏
 */
- (void)hideCustomNavView {
    [self.navView removeFromSuperview];
}
/**
 显示自定义导航栏右按钮
 */
- (void)showLeftBackItem {
    self.navView.navBackButton.hidden = NO;
}
/**
 隐藏自定义导航栏右按钮
 */
- (void)hideLeftBackItem {
    self.navView.navBackButton.hidden = YES;
}

#pragma mark - *********************子类可选实现方法*********************

#pragma mark - 子类基本配置

- (void)configuration {}

- (void)initSubViews {}

- (void)autoLayoutForSubViews {}

- (void)bindViewModel{}

- (void)fetchData {}

#pragma mark - *********************Setter&&Geter*********************

#pragma mark - Setter
/**
 显示/隐藏空数据界面
 @param showEmptyDataView <#showEmptyDataView description#>
 */
- (void)setShowEmptyDataView:(BOOL)showEmptyDataView {
    self.emptyDataView.hidden = !showEmptyDataView;
}

/**
 自定义导航标题
 @param navTitle 导航栏标题
 */
- (void)setNavTitle:(NSString *)navTitle {
    self.navView.titleLabel.text = navTitle;
}
/**
 自定义导航栏下划线
 @param showNavLine 下划线
 */
- (void)setShowNavLine:(BOOL)showNavLine {
    if (!showNavLine) {
        self.lineView.hidden = YES;
    }
}

#pragma mark - Getter
/**
 自定义导航栏
 @return 导航栏视图
 */
- (BaseNaviView *)navView {
    if (!_navView) {
        _navView = [BaseNaviView new];
    }
    return _navView;
}
/**
 没有数据提示页面
 @return 没有数据提示页面
 */
- (BaseEmptyDataView *)emptyDataView {
    if (!_emptyDataView) {
        //提示背景
        _emptyDataView = [BaseEmptyDataView new];
        
        [self.view addSubview:_emptyDataView];
        [_emptyDataView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(@0);
        }];
    }
    return _emptyDataView;
}
/**
 加载等待视图
 */
- (CustomLoadingView *)loadingView {
    if (!_loadingView) {
        _loadingView = [CustomLoadingView new];
    }
    return _loadingView;
}

@end

